package com.rays.sysmech.core;

import java.awt.Desktop;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;


public class FileData {
	
	File file;
	
	public FileData(File iFile)
	{
		if(iFile.isFile())
		{
			this.file = iFile;
		}
		else
		{
			//Throw exception
		}
	}
	
	public String getName()
	{
		return this.file.getName();
	}
	
	public long getSize()
	{
		return this.file.length();
	}
	
	public String getType()
	{
		return this.file.getName();
	}
	
	public String getPath()
	{
		return this.file.getAbsolutePath();
	}
	
	@Override
	public boolean equals(Object obj) {
		
		Boolean returnVal = false;
		if(obj instanceof FileData)
		{
			
			FileData iFileData = (FileData)obj; 
			returnVal = this.getName().equalsIgnoreCase(iFileData.getName()) && 
						this.getSize() == iFileData.getSize() &&
						this.getType().equalsIgnoreCase(iFileData.getType()) &&
						this.getCheckSum(this.file) == this.getCheckSum(iFileData.file);			
		}
		
		return returnVal;
	}
	
	private long getCheckSum(File file)
	{
		/*FileInputStream inptStrm = null;
		try {
			inptStrm = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        CheckedInputStream check = 
          new CheckedInputStream(inptStrm, new CRC32());
        BufferedInputStream in = 
          new BufferedInputStream(check);
        try {
			while (in.read() != -1) {
			    // Read file in completely
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
        
		//return check.getChecksum().getValue();
		return 0;
	}
	
	@Override
	public int hashCode() {
		
		return this.getName().hashCode() ^ (int)this.getSize() ^ this.getType().hashCode() ^ (int)this.getCheckSum(this.file);
	}
	
	public void openFile() throws IOException
	{
		Desktop.getDesktop().open(this.file);
	}

}
